package com.ruoyi.project.men.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.men.domain.MenSildeDO;
import com.ruoyi.project.men.service.MenSildeService;
import com.ruoyi.project.wx.domain.GoodsGoodsDO;
import com.ruoyi.project.wx.service.GoodsGoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 门户图片Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/men/menSilde")
public class MenSildeController extends BaseController
{
	@Autowired
	private MenSildeService menSildeService;
	@Autowired
	private GoodsGoodsService goodsGoodsService;


	/*@GetMapping("/{id}")
	@RequiresPermissions("men:menSilde:menSilde")
	String MenSilde(@PathVariable("id") String id,Model model){
		if(StrUtil.isNotBlank(id)){
			String[] split = id.split("&");
			model.addAttribute("linkId",split[0]);
			model.addAttribute("status",split[1]);

		}
		return "men/menSilde/menSilde";
	}*/

	/**
	 * 查询门户图片列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menSilde:list')")
	@GetMapping("/list")
	public TableDataInfo list(MenSildeDO menSilde)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menSilde);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","ca_at");
		stringObjectMap.put("order","desc");
		stringObjectMap.put("status",menSilde.getStatus()+"");
		List<MenSildeDO> menSildeList = menSildeService.likeList(stringObjectMap);
		if (menSildeList.size()>0){
			for (MenSildeDO menSildeDo:menSildeList){
				String goodsId = menSildeDo.getGoodsId();
				GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(goodsId);
				if (goodsGoodsDO!=null){
					menSildeDo.setGoodsId(goodsGoodsDO.getName());
				}

			}
		}
		return getDataTable(menSildeList);
	}

	/**
	 * 导出门户图片列表
	 */
	@PreAuthorize("@ss.hasPermi('men:menSilde:export')")
	@Log(title = "门户图片", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MenSildeDO menSilde)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menSilde);
		List<MenSildeDO> menSildeList = menSildeService.list(stringObjectMap);
		if (menSildeList.size()>0){
			for (MenSildeDO menSildeDo:menSildeList){
				String goodsId = menSildeDo.getGoodsId();
				GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(goodsId);
				if (goodsGoodsDO!=null){
					menSildeDo.setGoodsId(goodsGoodsDO.getName());
				}

			}
		}
		ExcelUtil<MenSildeDO> util = new ExcelUtil<MenSildeDO>(MenSildeDO.class);
		return util.exportExcel(menSildeList, "menSilde");
	}

	/**
	 * 获取门户图片详细信息
	 */
	@PreAuthorize("@ss.hasPermi('men:menSilde:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{

		String[] split = id.split("&");
		AjaxResult ajaxResult = AjaxResult.success();
		Map<String,Object> map = new HashMap<>();
		map.put("sort","gmt_create");
		map.put("order","desc");
		map.put("del_flag","0");
		map.put("disabled","0");
		if(split.length>=2){
			map.put("name",split[1]);
		}

		ajaxResult.put("goodsList",goodsGoodsService.list(map));
		ajaxResult.put("data",menSildeService.get(split[0]));

		return ajaxResult;
	}

	/**
	 * 新增门户图片
	 */
	@PreAuthorize("@ss.hasPermi('men:menSilde:add')")
	@Log(title = "门户图片", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MenSildeDO menSilde)
	{
		menSilde.setId(IdUtil.simpleUUID());
		menSilde.setDelFlag(0);
		menSilde.setCaAt(new Date());
		return toAjax(menSildeService.save(menSilde));
	}

	/**
	 * 修改门户图片
	 */
	@PreAuthorize("@ss.hasPermi('men:menSilde:edit')")
	@Log(title = "门户图片", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MenSildeDO menSilde)
	{
		menSilde.setUpAt(new Date());
		return toAjax(menSildeService.update(menSilde));
	}

	/**
	 * 删除门户图片
	 */
	@PreAuthorize("@ss.hasPermi('men:menSilde:remove')")
	@Log(title = "门户图片", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MenSildeDO menSilde = menSildeService.get(string);
				menSilde.setDelFlag(1);
				menSilde.setUpAt(new Date());
				menSildeService.update(menSilde);
			}
		}
		return toAjax(ids.length);
		//return toAjax(menSildeService.deleteMenSildeByIds(ids));
	}

	@GetMapping("/add/{id}")
	@RequiresPermissions("men:menSilde:add")
	String add(@PathVariable("id") String id,Model model){
		if(StrUtil.isNotBlank(id)){
			String[] split = id.split("&");
			model.addAttribute("linkId",split[0]);
			model.addAttribute("status",split[1]);

		}
		return "men/menSilde/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("men:menSilde:edit")
	String edit(@PathVariable("id") String id, Model model){
		MenSildeDO menSilde = menSildeService.get(id);
		model.addAttribute("menSilde", menSilde);
		List<Map<String,Object>> list = goodsGoodsService.queryAllForIdAndName(new HashMap<String,Object>());
		model.addAttribute("goodsList", list);
		return "men/menSilde/edit";
	}
}
